Imports Col = System.Collections
Imports Gen = System.Collections.Generic
Imports Ref = System.Reflection
Imports Sys = System

Namespace Enhancements.Core.Logging
    ''' <summary>Represents a collection of writer instances.</summary>
    <DebuggerStepThrough()> _
    Public NotInheritable Class WriterCollection
        Implements Col.IEnumerable

        Private __Collection As Gen.List(Of Writer) = New Gen.List(Of Writer)

        Friend Sub New()
        End Sub

        ''' <summary>Adds a writer to the collection.</summary>
        ''' <param name="_writer">Writer to add.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_writer is null.</exception>
        ''' <exception cref="IsTrueException">The writer is already in the collection.</exception>
        Public Sub Add(ByVal _writer As Writer)
            ___Assert.___IsDisposedNot("WriterCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_writer", _writer, True)
            ___Assert.___IsTrueNot("__Collection.Contains(_writer)", __Collection.Contains(_writer), True)

            __Collection.Add(_writer)
        End Sub

        ''' <summary>Clears the collection.</summary>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        Public Sub Clear()
            ___Assert.___IsDisposedNot("WriterCollection[instance]", Me.___DisposeCalled, True)

            __Collection.Clear()
        End Sub

        ''' <summary>Removes a writer from the collection.</summary>
        ''' <param name="_writer">Writer to remove.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_writer is null.</exception>
        ''' <exception cref="IsTrueNotException">The writer is not in the collection.</exception>
        Public Sub Remove(ByVal _writer As Writer)
            ___Assert.___IsDisposedNot("WriterCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_writer", _writer, True)
            ___Assert.___IsTrue("__Collection.Contains(_writer)", __Collection.Contains(_writer), True)

            __Collection.Remove(_writer)
        End Sub

        ''' <summary>Removes a writer from the collection.</summary>
        ''' <param name="_index">Index of the writer to remove.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsInRangeNotException">_index is invalid.</exception>
        Public Sub RemoveAt(ByVal _index As Sys.Int32)
            ___Assert.___IsDisposedNot("WriterCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsInRange("__Collection.Count", __Collection.Count, 1, Sys.Int32.MaxValue, True)
            ___Assert.___IsInRange("_index", _index, 0, __Collection.Count - 1, True)

            __Collection.RemoveAt(_index)
        End Sub

        ''' <summary>Gets a writer of the collection.</summary>
        ''' <param name="_index">Index of the writer to get.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsInRangeNotException">_index is invalid.</exception>
        Default Public ReadOnly Property Item(ByVal _index As Sys.Int32) As Writer
            Get
                ___Assert.___IsDisposedNot("WriterCollection[instance]", Me.___DisposeCalled, True)
                ___Assert.___IsInRange("__Collection.Count", __Collection.Count, 1, Sys.Int32.MaxValue, True)
                ___Assert.___IsInRange("_index", _index, 0, __Collection.Count - 1, True)

                Return __Collection(_index)
            End Get
        End Property

        ''' <summary>Gets the writers count in the collection.</summary>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        Public ReadOnly Property Count() As Int32
            Get
                ___Assert.___IsDisposedNot("WriterCollection[instance]", Me.___DisposeCalled, True)

                Return __Collection.Count
            End Get
        End Property

        ''' <summary>Gets the instance IEnumerable.GetEnumerator implementation.</summary>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <returns> </returns>
        Public Function GetEnumerator() As Col.IEnumerator Implements Col.IEnumerable.GetEnumerator
            ___Assert.___IsDisposedNot("WriterCollection[instance]", Me.___DisposeCalled, True)

            Return __Collection.GetEnumerator()
        End Function

        Friend Sub ___Write(ByVal _record As ___Record)
            ___Assert.___IsDisposedNot("WriterCollection[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_record", _record, True)
            ___Assert.___IsEmptyNot("_record.___UnformattedIdentifier", _record.___UnformattedIdentifier, True)
            ___Assert.___IsNullNot("_record.___UnformattedCallingProcedure", _record.___UnformattedCallingProcedure, True)
            ___Assert.___IsNullNot("_record.___UnformattedObjects", _record.___UnformattedObjects, True)

            For Each _collectionWriter As Writer In __Collection
                _collectionWriter.___Write(_record)
            Next _collectionWriter
        End Sub

        Private __DisposeCalled As System.Boolean = False
        Friend Property ___DisposeCalled() As System.Boolean
            Get
                Return __DisposeCalled
            End Get
            Private Set(ByVal _value As System.Boolean)
                ___Assert.___IsDisposedNot("WriterCollection[instance]", ___DisposeCalled, True)
                ___Assert.___IsTrue("___DisposeCalled._value", _value, True)

                __DisposeCalled = _value
            End Set
        End Property
        Friend Sub ___Dispose()
            ___Assert.___IsDisposedNot("WriterCollection[instance]", ___DisposeCalled, True)
            ___DisposeCalled = True

            For Each _collectionWriter As Writer In __Collection
                _collectionWriter.___Dispose()
            Next _collectionWriter
            __Collection.Clear()
        End Sub

    End Class
End Namespace
